package tbalert.web.entity;

import org.apache.commons.lang.StringUtils;

public class Process {
	public static final String SUCCESS_PAGE = "success";

	private void adjustForwardStep() {
		if (!StringUtils.isBlank(curStep) && !StringUtils.isBlank(chain)) {
			String[] steps = StringUtils.split(chain, '#');
			for (int i = 0; i < steps.length; i++) {
				if (StringUtils.equals(curStep, steps[i])) {
					forwardStep = (i + 1) < steps.length ? steps[i + 1] : SUCCESS_PAGE;
					return;
				}
			}
		}
	}

	private void adjustReverseStep() {
		if (!StringUtils.isBlank(curStep) && !StringUtils.isBlank(chain)) {
			String[] steps = StringUtils.split(chain, '#');
			for (int i = 1; i < steps.length; i++) {
				if (StringUtils.equals(curStep, steps[i])) {
					reverseStep = steps[i - 1];
					return;
				}
			}
		}
	}

	public void adjustChain(Blxxk blxxk) {
		if (blxxk != null) {
			chain = "baseInfo";
			if (blxxk.getWxdp() != null && blxxk.getWxdp()) {
				chain = chain + "#dpsyb";
			}
			if (blxxk.getWxhiv() != null && blxxk.getWxhiv()) {
				chain = chain + "#hivb";
			}
			chain = chain + "#fxpgb";
		} else {
			chain = null;
		}
		adjustReverseStep();
		adjustForwardStep();
	}

	public void adjustChain() {
		adjustReverseStep();
		adjustForwardStep();
	}

	private String chain;
	private String curStep;
	private String reverseStep;
	private String forwardStep;
	private String action;

	public String getNextStep() {
		if (StringUtils.equals(action, "reverse")) {
			return this.reverseStep;
		} else if (StringUtils.equals(action, "submit")) {
			return "submit.do";
		} else {
			return this.forwardStep;
		}
	}

	public String getChain() {
		return chain;
	}

	public void setChain(String chain) {
		this.chain = chain;
	}

	public String getCurStep() {
		return curStep;
	}

	public void setCurStep(String curStep) {
		this.curStep = curStep;
	}

	public String getReverseStep() {
		return reverseStep;
	}

	public void setReverseStep(String reverseStep) {
		this.reverseStep = reverseStep;
	}

	public String getForwardStep() {
		return forwardStep;
	}

	public void setForwardStep(String forwardStep) {
		this.forwardStep = forwardStep;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}
}
